package com.swfcamp.shopper.amazon
{
	import com.swfcamp.shopper.IProduct;
	import com.swfcamp.shopper.IProductProvider;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;

	public class AmazonProduct implements IProduct
	{
		[Bindable]
		public var title:String;
		[Bindable]
		public var id:String;
		[Bindable]
		public var provider:IProductProvider;
		[Bindable]
		public var imageUrl:String;
		[Bindable]
		public var attributes:Dictionary;
		[Bindable]
		public var price:String;
		[Bindable]
		public var url:String;
		
		private var loader:URLLoader = new URLLoader();
		
		private namespace AMZ = "http://webservices.amazon.com/AWSECommerceService/2005-10-05";
		
		public function AmazonProduct(asin:String, provider:AmazonProductProvider )
		{
			loader.dataFormat = "text";
			loader.addEventListener(Event.COMPLETE,productDetails,false,0,true);
			
			this.id = asin;
			this.provider = provider;
			
			var req:URLRequest = new URLRequest("http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId="+provider.apiKey+"&ResponseGroup=OfferFull,ItemAttributes,Images&Operation=ItemLookup&ItemId=" + asin);
			loader.load(req);
			
			
		}
		
		protected function productDetails(evt:Event):void{
			var xml:XML = XML(loader.data);
			this.title = String(xml..AMZ::Item.AMZ::ItemAttributes.AMZ::Title);
			this.price = String(xml..AMZ::ListPrice.AMZ::FormattedPrice);
			this.url = String(xml..AMZ::DetailPageURL);
			this.imageUrl = String(xml.AMZ::Items.AMZ::Item.AMZ::MediumImage.AMZ::URL[0]);
		}
		
		
	}
}